#!/bin/bash
cur_dir=`pwd`
name=`uname`

dst_dir=~/git/run   #for linux or freebsd installer
src=(clear_trash.sh cdd.sh  pwdd.sh dk.sh  vh.sh  filt.sh          see_code.sh     see_code.sh     s.sh sd.sh d2u.sh ostool.sh    i.sh g.sh lr.sh  a.sh gr.sh ct.sh sl.sh vlogin.sh)
dst=(clear_trash    cdd     pwdd    dk     vh     f.sh             sc              see_code.sh     s    sd    d      ostool.sh    i    g    lr     a    gr    ct    sl    vlogin.sh)

src=(show.sh psv.sh agit.sh ${src[@]} )
dst=(show    psv    agit    ${dst[@]} )

if [ ${#src[@]} -ne ${#dst[@]} ] ; then
    echo "-- error, item's size of src != item's size of dst"
    exit 1
fi
 
#for((i=0;i<${#src[@]};++i))
#do
#   printf "%-16s -> %s\n" ${src[$i]} ${dst[$i]}
#done

if [ "$name" == "Linux" ] || [ "$name" == "FreeBSD" ];then
    install_dir=$dst_dir/bin
    if [ ! -d "$install_dir" ] ; then
        mkdir -p  "$install_dir"
    fi

    _profile="$HOME/.profile"
    if [ ! -f $_profile ] ; then
        touch $_profile
    fi

    if ! grep -i "$dst_dir/bin" $_profile; then
        printf "export PATH=\$PATH:$dst_dir/bin\n" >> $_profile
        source $_profile
    fi

    src=(${src[*]} t.sh gnome/open6.sh)
    dst=(${dst[*]} t    o6)

elif [ $name == "Darwin" ];then
    #install_dir=/usr/local/bin
    install_dir=$HOME/run/bin

    mkdir -p $HOME/run/bin
    mkdir -p $HOME/run/tmp
    mkdir -p $HOME/run/etc
    mkdir -p $HOME/run/log
    mkdir -p $HOME/run/lib
    mkdir -p $HOME/run/dst

    src=(${src[*]} login_net.sh open3.osa open4.osa  cs.sh gr.sh sl.sh files_line.sh)
    dst=(${dst[*]} lg           o3        o4         cs    gr    sl    fl)
fi

echo "current computer platform is:$name"
echo "installed directory is: $install_dir"


prefix="-- "
if [ -z "$1" ]; then
    chmod u+x "$cur_dir"/*.sh
    for((i=0; i<${#src[*]}; i++)); do
        ln -s -f "$cur_dir/${src[i]}" "$install_dir/${dst[i]}"
        printf "%sinstall %2d %-16s as %s\n"  "$prefix" $i "${src[i]}"  "$install_dir/${dst[i]}"
    done
elif [ "$1" = "clean" ];then
    for((i=0; i<${#src[*]}; i++)); do
        if [ -f "$install_dir/${dst[i]}" ] ; then
            rm -f "$install_dir/${dst[i]}" 
            printf "%sclean %s\n" "$prefix" "$install_dir/${dst[i]}"
        fi
    done
fi


grep "mv_to_trash_rm.sh" $HOME/.profile >/dev/null
#not found
if [ $? -ne 0 ] ; then 
    cat >> $HOME/.profile <<EOF

#import mv_to_trash
source $HOME/git/tool_sh/mv_to_trash_rm.sh

alias rmi='mv_to_trash'
alias rm='mv_to_trash'
alias rm_='/bin/rm'

EOF
fi

if [ ! -f $HOME/.wgetrc ] ; then
    #make wget can download data from https
    echo "check-certificate=no" > $HOME/.wgetrc
fi
